package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.User;
import com.archerdu.apply.service.UserManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.commons.constant.ErrorConstant;

/**
 * 
 * @projectName: springDemo
 * @package: com.archerdu.apply.controller.admin
 * @className: MainController
 * @description: 管理平台入口
 * @author: archer du
 * @createDate: 2013-8-9 下午1:34:28
 * @updateUser: dxf
 * @updateDate: 2013-8-9 下午1:34:28
 * @updateRemark: 新建
 * @version: 0.0.1
 */
@Controller
@Scope("prototype")
@RequestMapping("/admin/userCenter")
public class UserCenterController extends BaseController {

    private static final long serialVersionUID = -2719764446417372805L;

    private User              user;

    private UserManager       userManager;

    /** 旧密码 */
    private String            oldPassword;

    /**
     * 管理平台首页
     * 
     * @return
     */
    @RequestMapping(value = "/main")
    public String main() {
        return "admin/userCenter/main";
    }

    /**
     * 跳转到修改密码页面
     * 
     * @return
     */
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.GET)
    public String modifyPasswordPage() {
        return "admin/userCenter/modifyPassword";
    }

    /**
     * 修改密码
     * 
     * @return
     */
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    public ModelAndView modifyPassword(UserCenterController ucc, HttpSession session) {
        ModelAndView mav = new ModelAndView();

        try {
            userManager.modifyPwd(ucc.getUser(),ucc.getOldPassword(), session);
            addMessage(ErrorConstant.PASSWORD_MODIFY_SUCCESS);
        } catch (BussinessException e) {
            addMessage(e.getMessage());
            logger.warn(e.getMessage(), e);
        } catch (Exception e) {
            addMessage(ErrorConstant.UNKONW_ERROR);
            logger.error(e.getMessage(), e);
        }

        mav.addObject(getMessage());
        mav.setViewName("admin/userCenter/modifyPassword");

        return mav;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }
}
